<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script>

    const cai = {
      uname: '只因',
      age: 2.5,
      hobbit: ['sing', 'dance', 'rap', 'basketball'],
      fans: {
        uname: 'ikun',
        age: 9999,
        hobbit: ['smoke', 'drink', 'makeHairStyle'],
      },
    }

    let newObj = {}
    function deepClone(newObj, oldObj) {
      for (let k in oldObj) {

        if (oldObj[k] instanceof Array) {

          newObj[k] = []
          // newObj.hobbit  = []
          // 不写[]相当于声明未赋值，undefined
          deepClone(newObj[k], oldObj[k])

        } else if (oldObj[k] instanceof Object) {

          newObj[k] = {}
          deepClone(newObj[k], oldObj[k])

        } else {
          newObj[k] = oldObj[k]
        }

      }

    }

    deepClone(newObj, cai)

    newObj.fans.hobbit[2] = '代码敲多了秃头'
    console.log(newObj);
    console.log(cai);
  </script>
</body>

</html>